package com.museum.utils;

import java.net.URI;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.museum.counst.URLS;
import com.museum.counst.USERS;
import com.museum.entities.action.ActionResponse;
import com.museum.entities.friend.FriendList;
import com.museum.entities.market.MarketList;
import com.museum.entities.process.MakeList;
import com.museum.entities.userinfo.UserInfo;
import com.museum.entities.useritem.UserItem;

public class Sentor {

	private static final Log log = LogFactory.getLog(Sentor.class);

	DefaultHttpClient client = new DefaultHttpClient();
	HttpPost post = Utils.createPost();

	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private Gson gson = new Gson();

	public FriendList friends() {
		try {
			log.debug("getting friends...");
			post.setURI(new URI(URLS.FRIENDS));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			log.debug("getting friends done!");
			return gson.fromJson(resjson, FriendList.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public MakeList makeList(String uid) {
		try {
			log.debug("getting process list of user " + uid);
			post.setURI(new URI(URLS.MAKELIST + uid));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			log.debug("getting process done!");
			return gson.fromJson(resjson, MakeList.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse make(int itemid, String uid) {
		try {
			log.debug("making item " + itemid + " at " + uid);
			post.setURI(new URI(URLS.make(itemid, uid)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			log.debug("making item done!");
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse makeDone(int itemid, String uid) {
		try {
			log.debug("fetching processed item " + itemid + " at " + uid);
			post.setURI(new URI(URLS.makedone(itemid, uid)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			log.debug("fetching processed item done!");
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse buy(String itemid, int q) {
		try {
			post.setURI(new URI(URLS.buy(itemid, q)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse use(String itemid) {
		try {
			post.setURI(new URI(URLS.use(itemid)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse sell(String flag) {
		try {
			post.setURI(new URI(URLS.sell(flag)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse dig(int x, int y) {
		try {
			post.setURI(new URI(URLS.dig(x, y)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse digBatch(int x, int y) {
		try {
			post.setURI(new URI(URLS.DIGBATCH));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse jiesuan() {
		try {
			post.setURI(new URI(URLS.JIESUAN));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse pohuai() {
		try {
			post.setURI(new URI(URLS.pohuai("", "")));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public MarketList marketList(int item, int page) {
		try {
			post.setURI(new URI(URLS.marketList(item, page)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, MarketList.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse xiajia(int id) {
		try {
			post.setURI(new URI(URLS.xiajia(id)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse cangbao(int item_id) {
		try {
			post.setURI(new URI(URLS.cangbao(item_id)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public UserItem myzlList() {
		try {
			post.setURI(new URI(URLS.ZLLIST));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, UserItem.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public UserItem mycwList() {
		try {
			post.setURI(new URI(URLS.CWLIST));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, UserItem.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public ActionResponse digDone() {
		try {
			post.setURI(new URI(URLS.DIGDONE));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, ActionResponse.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public UserInfo userInfo() {
		try {
			post.setURI(new URI(URLS.USERINFO));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, UserInfo.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public UserInfo friendInfo(
			String uid) {
		try {
			post.setURI(new URI(URLS.userinfo(uid)));
			post.setHeader("Cookie", USERS.cookies[0]);
			String resjson = Utils.trans(client.execute(post));
			return gson.fromJson(resjson, UserInfo.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) throws InterruptedException {
		Sentor s = new Sentor();
		 System.out.println(s.buy("596", 100).getData().getText());
	}
}
